package com.backstage.common.uitil;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Project authority
 * @Package com.backstage.common
 * @Data 2017/7/13下午 2:59.
 * @Aurhor 阮雪峰
 */
public class DateUtil {

    public static void main(String[] args) {
        System.out.println(compareTo("2016-10-27 17:47:57", "2016-10-28 17:47:57"));

		/*String now = "2016-11-04 18:39:20";

		try {
			System.out.println(DateCompare.afterNMinute(now,30));
		} catch (ParseException e) {
			e.printStackTrace();
		}*/
    }

    public static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private DateUtil(){}

    public static String getNowDate() {
        Date currentTime = new Date();
        String dateString = formatter.format(currentTime);
        ParsePosition pos = new ParsePosition(8);
        Date currentTime_2 = formatter.parse(dateString, pos);
        return currentTime_2.toString();
    }

    public static String getFormatDate(String date){
        return getFormatDate(new Date(date)).toString();
    }

    public static Date getFormatDate(Date date){
        String dateString = formatter.format(date);
        ParsePosition pos = new ParsePosition(8);
        Date currentTime_2 = formatter.parse(dateString, pos);
        return currentTime_2;
    }


    public static boolean isValidDate(String str) {
        return isValidDate(str,"yyyy-MM-dd HH:mm:ss");
    }

    public static boolean isValidDate(String str,String fromat) {
        boolean convertSuccess=true;
        // 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
        SimpleDateFormat format = new SimpleDateFormat(fromat);
        try {
            // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            // e.printStackTrace();
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            convertSuccess=false;
        }
        return convertSuccess;
    }

    /**
     *  0:两时间相等
     *  1:date1比date2晚
     * -1:date1比date2早
     * @param date1
     * @param date2
     * @return
     */
    public static int compareTo(String date1, String date2) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateTime1;
        Date dateTime2;
        try {
            dateTime1 = dateFormat.parse(date1);
            dateTime2 = dateFormat.parse(date2);
            int i = dateTime1.compareTo(dateTime2);
            return i;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String afterNDay(String date, int n) throws ParseException {
        Calendar c = Calendar.getInstance();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = df.parse(date);
        c.setTime(d);
        c.add(Calendar.DATE, n);
        Date d2 = c.getTime();
        String s = df.format(d2);
        return s;
    }

    public static String afterNHour(String date, int n) throws ParseException {
        Calendar c = Calendar.getInstance();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = df.parse(date);
        c.setTime(d);
        c.add(Calendar.HOUR, n);
        Date d2 = c.getTime();
        String s = df.format(d2);
        return s;
    }

    public static String afterNMinute(String date, int n) throws ParseException {
        Calendar c = Calendar.getInstance();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = df.parse(date);
        c.setTime(d);
        c.add(Calendar.MINUTE, n);
        Date d2 = c.getTime();
        String s = df.format(d2);
        return s;
    }
}
